From a2cc38dc7e8679f208be24f5360f0db927a76eb8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Jul 2016 10:29:10 +0300 Subject: [PATCH] Read target config later --- src/cargo/core/workspace.rs | 29 ++++++++++++++++------------ src/cargo/ops/cargo_install.rs | 2 +- src/cargo/ops/cargo_package.rs | 2 +- src/cargo/util/config.rs | 35 +++++++++++++--------------------- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index f4a07b923..d4c32e572 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -32,7 +32,8 @@ pub struct Workspace<'cfg> { // `current_manifest` was found on the filesystem with `[workspace]`. root_manifest: Option, - // Allows to override the target directory for the purposes of `cargo install`. + // Shared target directory for all the packages of this workspace. + // `None` if the default path of `root/target` should be used. target_dir: Option, // List of members in this workspace with a listing of all their manifest @@ -81,6 +82,8 @@ impl<'cfg> Workspace<'cfg> { /// before returning it, so `Ok` is only returned for valid workspaces. pub fn new(manifest_path: &Path, config: &'cfg Config) -> CargoResult> { + let target_dir = try!(config.target_dir()); + let mut ws = Workspace { config: config, current_manifest: manifest_path.to_path_buf(), @@ -89,7 +92,7 @@ impl<'cfg> Workspace<'cfg> { packages: HashMap::new(), }, root_manifest: None, - target_dir: None, + target_dir: target_dir, members: Vec::new(), }; ws.root_manifest = try!(ws.find_root(manifest_path)); @@ -107,7 +110,8 @@ impl<'cfg> Workspace<'cfg> { /// /// This is currently only used in niche situations like `cargo install` or /// `cargo package`. - pub fn one(package: Package, config: &'cfg Config, target_dir: Option) -> Workspace<'cfg> { + pub fn one(package: Package, config: &'cfg Config, target_dir: Option) + -> CargoResult> { let mut ws = Workspace { config: config, current_manifest: package.manifest_path().to_path_buf(), @@ -116,16 +120,21 @@ impl<'cfg> Workspace<'cfg> { packages: HashMap::new(), }, root_manifest: None, - target_dir: target_dir, + target_dir: None, members: Vec::new(), }; { let key = ws.current_manifest.parent().unwrap(); let package = MaybePackage::Package(package); ws.packages.packages.insert(key.to_path_buf(), package); + ws.target_dir = if let Some(dir) = target_dir { + Some(dir) + } else { + try!(ws.config.target_dir()) + }; ws.members.push(ws.current_manifest.clone()); } - return ws + return Ok(ws) } /// Returns the current package of this workspace. @@ -161,13 +170,9 @@ impl<'cfg> Workspace<'cfg> { } pub fn target_dir(&self) -> Filesystem { - if let Some(ref fs) = self.target_dir { - return fs.clone() - } - if let Some(fs) = self.config.target_dir() { - return fs.clone() - } - Filesystem::new(self.root().join("target")) + self.target_dir.clone().unwrap_or_else(|| { + Filesystem::new(self.root().join("target")) + }) } /// Returns the root [replace] section of this workspace. diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 093e95944..b120ace95 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -89,7 +89,7 @@ pub fn install(root: Option<&str>, Some(Filesystem::new(config.cwd().join("target-install"))) }; - let ws = Workspace::one(pkg, config, overidden_target_dir); + let ws = try!(Workspace::one(pkg, config, overidden_target_dir)); let pkg = try!(ws.current()); // Preflight checks to check up front whether we'll overwrite something. diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index f2a90a145..8201a5fa8 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -266,7 +266,7 @@ fn run_verify(ws: &Workspace, tar: &File, opts: &PackageOpts) -> CargoResult<()> let new_pkg = Package::new(new_manifest, &manifest_path); // Now that we've rewritten all our path dependencies, compile it! - let ws = Workspace::one(new_pkg, config, None); + let ws = try!(Workspace::one(new_pkg, config, None)); try!(ops::compile_ws(&ws, None, &ops::CompileOptions { config: config, jobs: opts.jobs, diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 32bbe7238..e3a33eab0 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -28,7 +28,6 @@ pub struct Config { values: LazyCell>, cwd: PathBuf, rustdoc: LazyCell, - target_dir: Option, extra_verbose: Cell, frozen: Cell, locked: Cell, @@ -37,23 +36,18 @@ pub struct Config { impl Config { pub fn new(shell: MultiShell, cwd: PathBuf, - homedir: PathBuf) -> CargoResult { - let mut cfg = Config { + homedir: PathBuf) -> Config { + Config { home_path: Filesystem::new(homedir), shell: RefCell::new(shell), rustc: LazyCell::new(), cwd: cwd, values: LazyCell::new(), rustdoc: LazyCell::new(), - target_dir: None, extra_verbose: Cell::new(false), frozen: Cell::new(false), locked: Cell::new(false), - }; - - try!(cfg.scrape_target_dir_config()); - - Ok(cfg) + } } pub fn default() -> CargoResult { @@ -65,7 +59,7 @@ impl Config { human("Cargo couldn't find your home directory. \ This probably means that $HOME was not set.") })); - Config::new(shell, cwd, homedir) + Ok(Config::new(shell, cwd, homedir)) } pub fn home(&self) -> &Filesystem { &self.home_path } @@ -108,8 +102,15 @@ impl Config { pub fn cwd(&self) -> &Path { &self.cwd } - pub fn target_dir(&self) -> Option<&Filesystem> { - self.target_dir.as_ref() + pub fn target_dir(&self) -> CargoResult> { + if let Some(dir) = env::var_os("CARGO_TARGET_DIR") { + Ok(Some(Filesystem::new(self.cwd.join(dir)))) + } else if let Some(val) = try!(self.get_path("build.target-dir")) { + let val = self.cwd.join(val.val); + Ok(Some(Filesystem::new(val))) + } else { + Ok(None) + } } fn get(&self, key: &str) -> CargoResult> { @@ -363,16 +364,6 @@ impl Config { } } - fn scrape_target_dir_config(&mut self) -> CargoResult<()> { - if let Some(dir) = env::var_os("CARGO_TARGET_DIR") { - self.target_dir = Some(Filesystem::new(self.cwd.join(dir))); - } else if let Some(val) = try!(self.get_path("build.target-dir")) { - let val = self.cwd.join(val.val); - self.target_dir = Some(Filesystem::new(val)); - } - Ok(()) - } - fn get_tool(&self, tool: &str) -> CargoResult { let var = tool.chars().flat_map(|c| c.to_uppercase()).collect::(); if let Some(tool_path) = env::var_os(&var) { -- 2.30.2